package com.controls.ws;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;

import com.Utility.WebServiceCommunicator.WebServiceFlag;
import com.events.WebServiceCommunicatorListener;
import com.objects.VaultFile;

public class VaultWSControl extends BaseWSControl {
	public VaultWSControl(Context cxt, WebServiceCommunicatorListener lsn) {
		super(cxt, lsn);
	}

	public boolean getWebInterfaceVaultUrl(String accessToken, String fileName) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<GetWebInterfaceVaultURL xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "<VaultFileName>"
				+ fileName + "</VaultFileName>" + "</GetWebInterfaceVaultURL>"
				+ "</soap:Body>" + "</soap:Envelope>";

		fetch(WebServiceFlag.GET_WEB_INTERFACE_VAULT_URL, content);
		return true;
	}

	public boolean getVaultCatalog(String accessToken) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<GetVaultCatalog xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "</GetVaultCatalog>"
				+ "</soap:Body>" + "</soap:Envelope>";

		fetch(WebServiceFlag.GET_VAULT_CATALOG, content);
		return true;
	}

	public boolean deleteFile(String accessToken, String file) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<DeleteFilesFromVault xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "<Files>" + file
				+ "</Files>" + "</DeleteFilesFromVault>" + "</soap:Body>"
				+ "</soap:Envelope>";

		fetch(WebServiceFlag.DELETE_VAULT_FILE, content);
		return true;
	}

	public boolean getVaultStatistics(String accessToken) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<GetVaultStatistics xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>"
				+ "</GetVaultStatistics>" + "</soap:Body>" + "</soap:Envelope>";

		fetch(WebServiceFlag.GET_VAULT_STATISTIC, content);
		return true;
	}

	public String parseAction(String xml, String key) {
		String returnValue = "";
		Document doc = getDomElement(xml);
		if (doc == null) {
			return returnValue;
		}

		NodeList results = doc.getElementsByTagName(key);
		JSONObject mObj = null;
		if (results.getLength() > 0) {
			Node node = results.item(0);
			try {
				mObj = new JSONObject(node.getTextContent());
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (mObj != null) {
			try {

				if (mObj.has("ReturnValue")) {
					returnValue = mObj.getString("ReturnValue");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return returnValue;
	}

	public ArrayList<VaultFile> parseVaultFile(String xml) {
		ArrayList<VaultFile> items = new ArrayList<VaultFile>();
		Document doc = getDomElement(xml);
		if (doc == null) {
			return items;
		}

		NodeList results = doc.getElementsByTagName("GetVaultCatalogResponse");
		JSONArray mArray = null;
		if (results.getLength() > 0) {
			Node node = results.item(0);
			try {
				mArray = new JSONArray(node.getTextContent());
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (mArray != null) {
			for (int i = 0; i < mArray.length(); i++) {
				try {
					JSONObject obj = mArray.getJSONObject(i);
					VaultFile item = new VaultFile();
					if (obj.has("FileName")) {
						item.setName(obj.getString("FileName"));
					}
					if (obj.has("FileSize")) {
						item.setSize(obj.getString("FileSize"));

					}
					if (obj.has("FileDate")) {
						item.setDate(obj.getString("FileDate"));

					}
					items.add(item);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
		return items;
	}

	public JSONObject parseVaultStatistics(String xml) {
		JSONObject item = null;
		Document doc = getDomElement(xml);
		if (doc == null) {
			return item;
		}

		NodeList results = doc.getElementsByTagName("GetVaultStatisticsResult");
		if (results.getLength() > 0) {
			Node node = results.item(0);
			try {
				item = new JSONObject(node.getTextContent());
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return item;
	}
}
